package request

import (
    "encoding/json"
    _ "github.com/go-sql-driver/mysql"
    "log"
    "net/http"
    "roy/common/data"
)

func Main1() {
    http.HandleFunc("/login1", loginGet)
    http.HandleFunc("/login2", loginPost)
    http.HandleFunc("/district", getDistrict)
    err := http.ListenAndServe("0.0.0.0:8080", nil)
    if err != nil {
        return
    }
}

type Result struct {
    Code    int
    Message string
    Data    []map[string]interface{}
}

type Auth struct {
    Username string `json:"username"`
    Password string `json:"password"`
}

type Where struct {
    Code string `json:"code"`
    Name string `json:"name"`
}

// loginGet get 请求登录
func loginGet(writer http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        return
    }
    username, uErr := r.Form["username"]
    password, pErr := r.Form["password"]
    var result Result
    if !uErr || !pErr {
        result.Code = 400
        result.Message = "用户名和密码不能为空"
    } else if username[0] == "admin" && password[0] == "123456" {
        result.Code = 200
        result.Message = "登录成功"
        user := make(map[string]interface{})
        user["username"] = username[0]
        user["nickname"] = "管理员"
        users := make([]map[string]interface{}, 0)
        users = append(users, user)
        result.Data = users
    } else {
        result.Code = 444
        result.Message = "用户名或密码错误"
    }
    if err = json.NewEncoder(writer).Encode(result); err != nil {
        log.Fatal(err)
    }
}

// loginPost post 请求登录
func loginPost(writer http.ResponseWriter, r *http.Request) {
    var auth Auth
    if err := json.NewDecoder(r.Body).Decode(&auth); err != nil {
        err = r.Body.Close()
        if err != nil {
            return
        }
        log.Fatal(err)
    }
    var result Result
    if auth.Username == "admin" && auth.Password == "123456" {
        result.Code = 200
        result.Message = "登录成功"
        user := make(map[string]interface{})
        user["username"] = auth.Username
        user["nickname"] = "管理员"
        users := make([]map[string]interface{}, 0)
        users = append(users, user)
        result.Data = users
    } else {
        result.Code = 400
        result.Message = "用户名或密码错误"
    }
    if err := json.NewEncoder(writer).Encode(result); err != nil {
        log.Fatal(err)
    }
}

// getDistrict 获取区划
func getDistrict(writer http.ResponseWriter, r *http.Request) {
    var where Where
    if err := json.NewDecoder(r.Body).Decode(&where); err != nil {
        err = r.Body.Close()
        if err != nil {
            return
        }
        log.Fatal(err)
    }
    strSql := "select code,name from yunduan_district where 1"
    if where.Code != "" {
        strSql = strSql + " and code like '%" + where.Code + "%'"
    }
    if where.Name != "" {
        strSql = strSql + " and name like '%" + where.Name + "%'"
    }
    rows := data.GetRows(strSql)
    arr, err := data.Rows2Array(rows)
    if err != nil {
        log.Fatal(err)
        return
    }
    var result Result
    if arr == nil {
        result.Code = 300
        result.Message = "暂无数据"
    } else {
        result.Code = 200
        result.Message = "获取成功"
    }
    result.Data = arr
    if err = json.NewEncoder(writer).Encode(result); err != nil {
        log.Fatal(err)
    }
}
